#include<bits/stdc++.h>
#define ll long long
using namespace std;

int _,n,m,p,a[10000005],b[10000005],c[10000005];
int A,B,C,M;
int L,R,X,Y,f[10000005],g[10000005];

inline bool check()
{
    for(int i=1;i<=p;i++)
    {
        if(a[c[i]-1]==a[c[i]])
            f[i]=X;
        else
            f[i]=X+X;
    }

    for(int i=1;i<=m;i++)
        g[i]=1000000;

    for(int i=1;i<=p;i++)
    {
        if(a[c[i]-1]==a[c[i]])
            g[a[c[i]]]-=f[i];
        else
            f[i]=max(0,f[i]-g[a[c[i]-1]]),g[a[c[i]]]-=f[i];
        if(g[a[c[i]]]<0)
            return 0;
    }

    return 1;
}

inline void sol()
{
    cin>>n>>a[0]>>A>>B>>C>>M;
    for(int i=1;i<=n;i++)
        a[i]=(1LL*A*a[i-1]%M*a[i-1]%M+1LL*B*a[i-1]%M+1LL*C%M)%M+1;
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
        b[i]=a[i];
    m=unique(b+1,b+n+1)-b-1;
    for(int i=1;i<=n;i++)
        a[i]=lower_bound(b+1,b+m+1,a[i])-b;
    
    a[0]=p=0;
    for(int i=2;i<=n;i++)
    {
        if(a[i-2]!=a[i-1]&&a[i-1]==a[i])
            c[++p]=i;
        if(a[i-1]!=a[i])
            c[++p]=i;
    }

    L=Y=0,R=1000000;
    for(int __=1;__<=20;__++)
    {
        X=(L+R)>>1;
        if(check())
            Y=X,L=X+1;
        else
            R=X-1;
    }
    cout<<fixed<<setprecision(9)<<Y/1000000.0<<" 0\n";
}

int main()
{
    freopen("guess.in","r",stdin);
    freopen("guess.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);

    cin>>_;
    while(_--)
        sol();

    return 0;
}